package com.westa.smallshop.data.source.remote;

import android.content.Context;
import android.support.annotation.Nullable;

import com.alibaba.fastjson.JSONObject;
import com.android.volley.Request;
import com.android.volley.Response;
import com.westa.smallshop.data.HttpError;
import com.westa.smallshop.data.MyUser;
import com.westa.smallshop.data.User;
import com.westa.smallshop.data.source.AccountDataSource;
import com.westa.smallshop.util.HttpUtils;
import com.westa.smallshop.util.UserSPUtils;
import com.westa.smallshop.util.volley.FastJsonRequest;
import com.westa.smallshop.util.volley.VolleySingleton;

import timber.log.Timber;

/**
 * Created by cy on 11/3/16.
 */

public class AccountRemoteDataSource implements AccountDataSource {

    private static final String ACCOUNT_TAG = "account_tag";

    private static final String REQUEST_LOGIN_URL = HttpUtils.getHost() + "login";

    private static final String REQUEST_REGISTER_URL = HttpUtils.getHost() + "register";

    private Context mContext;

    public AccountRemoteDataSource(Context context){
        mContext = context;
    }

    @Override
    public void login(final String account, String password, final LoadCallBack<JSONObject> loadCallBack) {

        FastJsonRequest<JSONObject> request = new FastJsonRequest<>(Request.Method.GET, REQUEST_LOGIN_URL,
                JSONObject.class,
                new Response.Listener<JSONObject>() {

                    @Override
                    public void onResponse(JSONObject response) {
                        User user = new User();
                        user.setAccount(account);
                        UserSPUtils.saveUserToSp(user, mContext);
                        loadCallBack.onLoadDataSuccessful(response);
                    }
                },
                new HttpUtils.ErrorListener(mContext) {
                    @Override
                    public void onErrorData(@Nullable HttpError httpError) {
                        loadCallBack.onLoadDataFailed();
                    }
                });

        request.addParam("account", account);
        request.addParam("password", password);
        Timber.e(request.getUrl());
        VolleySingleton.addRequest(request, ACCOUNT_TAG);
    }

    @Override
    public void register(final String account, String password, final LoadCallBack<JSONObject> loadCallBack) {
        FastJsonRequest<JSONObject> request = new FastJsonRequest<>(Request.Method.GET, REQUEST_REGISTER_URL,
                JSONObject.class,
                new Response.Listener<JSONObject>() {

                    @Override
                    public void onResponse(JSONObject response) {
                        loadCallBack.onLoadDataSuccessful(response);
                    }
                },
                new HttpUtils.ErrorListener(mContext) {
                    @Override
                    public void onErrorData(@Nullable HttpError httpError) {
                        loadCallBack.onLoadDataFailed();
                    }
                });

        request.addParam("account", account);
        request.addParam("password", password);
        Timber.e(request.getUrl());
        VolleySingleton.addRequest(request, ACCOUNT_TAG);

    }

    @Override
    public void cancelLoadRequest() {
        VolleySingleton.cancelRequestForStartWithTag(ACCOUNT_TAG);
    }
}
